package com.topvision.camerasdk.album.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.ImageView;

/**
 * Created by lucy on 2017/5/5.
 */

public class ThumbnailView extends ImageView implements ViewTreeObserver.OnGlobalLayoutListener {


    public Canvas mCanvas;
    private WindowManager windowManager;
    private int width;
    private int height;
    private Paint mPaint;

    public ThumbnailView(Context context) {
        this(context,null);
    }

    public ThumbnailView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ThumbnailView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //得到屏幕宽高
        windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        width = windowManager.getDefaultDisplay().getWidth();
        height = windowManager.getDefaultDisplay().getHeight();

        initPaint();
    }
    private void initPaint() {
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(3);
        mPaint.setColor(Color.RED);
    }

    /* 小图的真实宽高 */
    public int realWidth;
    public int realHeight;
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Drawable drawable = getDrawable();
        if (drawable != null) {
            //获取图片的宽与高
            int intrinsicHeight = drawable.getIntrinsicHeight();
            int intrinsicWidth = drawable.getIntrinsicWidth();
            realWidth = intrinsicWidth / 4;
            realHeight = intrinsicHeight / 4;
            setMeasuredDimension(realWidth, realHeight);
        } else {
            setMeasuredDimension(widthMeasureSpec / 5, widthMeasureSpec / 5);
        }

    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    private boolean isFirstDraw = true;
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        this.mCanvas = canvas;
        if (listener != null) {
            listener.onThumbnail(canvas);
        }
        if (isFirstDraw && realWidth > 0 && realHeight > 0) {
            isFirstDraw = false;
            canvas.drawRect(new Rect(0,0,realWidth,realHeight),mPaint);
        }
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        //注册OnGlobalLayoutListener
        getViewTreeObserver().addOnGlobalLayoutListener(this);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        //注销OnGlobalLayoutListener
        getViewTreeObserver().removeOnGlobalLayoutListener(this);
    }

    private boolean isFirst = false;
    @Override
    public void onGlobalLayout() {
        //获取加载完后的图片
        if (!isFirst) {
            //得到图片
            Drawable drawable = getDrawable();
            if (drawable == null) {
                return;
            }
            //获取图片的宽与高
            int intrinsicHeight = drawable.getIntrinsicHeight();
            int intrinsicWidth = drawable.getIntrinsicWidth();
            isFirst = true;
        }
    }
    public ThumbnailListener listener = null;

    //回调接口
    public interface ThumbnailListener {
        //回调的方法
        void onThumbnail(Canvas canvas);
    }

    //声明回调接口
    public void setOnThumbnailListener(ThumbnailListener thumbnailListener) {
        listener = thumbnailListener;
    }
}
